<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="签约日期-始">
        <el-date-picker
          type="date"
          placeholder="请选择开始时间"
          v-model="queryParams.signerTimeBegin"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="签约日期-止">
        <el-date-picker
          type="date"
          placeholder="请选择结束时间"
          v-model="queryParams.signerTimeEnd"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="合同编号">
        <el-input
          v-model="queryParams.contractCode"
          placeholder="请输入合同编号"
          clearable
        />
      </el-form-item>
      <el-form-item label="学员姓名">
        <el-input
          v-model="queryParams.signerName"
          placeholder="请输入学员姓名"
          clearable
        />
      </el-form-item>

      <el-form-item label="年级">
        <el-select
          v-model="queryParams.grade"
          placeholder="请选择年级"
          clearable
        >
          <el-option
            v-for="item in gradeOpiton"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属校区">
        <campusSelect v-model="queryParams.campus" class="form-select" />
      </el-form-item>
      <el-form-item label="合同属性">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择合同属性"
          clearable
        >
          <el-option label="新签" :value="0" />
          <el-option label="续费" :value="1" />
          <el-option label="赠送" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="合同状态">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择合同状态"
          clearable
        >
          <el-option label="执行中" :value="0" />
          <el-option label="停课" :value="1" />
          <el-option label="冻结" :value="2" />
          <el-option label="已完结" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="合同类型">
        <el-select
          v-model="queryParams.contractConfirmStatus"
          placeholder="请选择确认状态"
          clearable
        >
          <el-option
            v-for="item in dictType"
            :label="item.dictLabel"
            :value="item.dictCode"
            :key="item.dictCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="确认状态">
        <el-select
          v-model="queryParams.contractConfirmStatus"
          placeholder="请选择确认状态"
          clearable
        >
          <el-option label="新合同" :value="0" />
          <el-option label="待出纳确认" :value="1" />
          <el-option label="出纳已确认" :value="2" />
          <el-option label="校长已确认" :value="3" />
          <el-option label="已锁定" :value="4" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="是否推荐">
        <el-select
          v-model="queryParams.isReferrer"
          placeholder="请选择是否推荐"
          clearable
        >
          <el-option label="是" :value="1" />
          <el-option label="否" :value="0" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="推荐人">
        <el-input
          v-model="queryParams.referrerId"
          placeholder="请输入推荐人"
          clearable
        />
      </el-form-item>
      <el-form-item label="是否折扣">
        <el-select
          v-model="queryParams.isReferrer"
          placeholder="请选择是否折扣"
          clearable
        >
          <el-option label="是" :value="1" />
          <el-option label="否" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="班主任">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入班主任"
          clearable
        />
      </el-form-item>
      <el-form-item label="出纳确认-始">
        <el-date-picker
          type="date"
          placeholder="请选择开始时间"
          v-model="queryParams.cashierApproveTimeBegin"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="出纳确认-止">
        <el-date-picker
          type="date"
          placeholder="请选择结束时间"
          v-model="queryParams.cashierApproveTimeEnd"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="签约者">
        <el-input
          v-model="queryParams.signerName"
          placeholder="请输入签约者"
          clearable
        />
      </el-form-item>
      <el-form-item label="在读状态">
        <el-select
          v-model="queryParams.isStudying"
          placeholder="请选择在读状态"
          clearable
        >
          <el-option label="是" :value="1" />
          <el-option label="否" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="contractList">
      <el-table-column
        label="编号"
        align="center"
        prop="contractCode"
        min-width="100"
      />
      <el-table-column label="姓名" align="center" prop="customerName" />
      <el-table-column label="所属校区" align="center" prop="campusName" />
      <el-table-column label="合同属性" align="center">
        <template slot-scope="scope">
          <span>{{
            scope.row.contractAttribute == 0
              ? "新签"
              : scope.row.contractAttribute == 1
              ? "续签"
              : "赠送"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="合同类型"
        align="center"
        prop="contractTypeName"
        min-width="120"
      />
      <el-table-column label="年级" align="center" prop="grade">
        <template #default="{ row }">
          <span>{{ formatGrade(row.grade) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同金额" align="center" prop="contractMoney" />
      <el-table-column label="在读状态" align="center">
        <template slot-scope="scope">
          <i
            class="el-icon-check"
            style="color: green"
            v-if="scope.row.isStudying == 1"
          ></i>
          <i class="el-icon-close" style="color: red" v-else></i>
        </template>
      </el-table-column>
      <el-table-column label="班主任" align="center" prop="techerName" />
      <el-table-column label="剩余课时" align="center" prop="remaining_class" />
      <el-table-column label="备注" align="center" prop="note" />
      <el-table-column
        label="开始日期"
        align="center"
        prop="beginTime"
        min-width="120"
      />
      <el-table-column label="周数" align="center" prop="week" />
      <el-table-column label="签约者" align="center" prop="signerName" />
      <el-table-column
        label="签约日期"
        align="center"
        prop="signerTime"
        min-width="120"
      />
      <el-table-column label="合同状态" align="center">
        <template #default="{ row }">
          <el-tag type="success" v-if="row.status === 0">执行中</el-tag>
          <el-tag type="danger" v-else-if="row.status === 1">停课</el-tag>
          <el-tag type="warning" v-else-if="row.status === 2">冻结</el-tag>
          <el-tag type="info" v-else>已完结</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="确认状态"
        align="center"
        prop="contractConfirmStatus"
        min-width="100"
      >
        <template slot-scope="scope">
          <span>{{
            scope.row.contractConfirmStatus == 0
              ? "新合同"
              : scope.row.contractConfirmStatus == 1
              ? "待出纳确认"
              : scope.row.contractConfirmStatus == 2
              ? "出纳已确认"
              : scope.row.contractConfirmStatus == 3
              ? "校长已确认"
              : scope.row.contractConfirmStatus == 4
              ? "已锁定"
              : ""
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="校长确认时间"
        align="center"
        prop="principalApproveTime"
        min-width="120"
      />
      <el-table-column
        label="出纳确认时间"
        align="center"
        prop="cashierApproveTime"
        min-width="120"
      />
      <el-table-column label="推荐人" align="center" prop="referrerName" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleDetail(scope.row)"
          >
            详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合同对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="线索客户" prop="customerId">
          <el-select
            v-model="form.customerId"
            placeholder="请选择线索客户"
            class="form-select"
            filterable
            clearable
          >
            <el-option
              v-for="item in customerList"
              :key="item.customerId"
              :label="`${item.customerCode} - ${item.customerName}`"
              :value="item.customerId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="合同编号" prop="contractCode">
          <el-input
            v-model="form.contractCode"
            placeholder="请输入合同编号"
            class="form-input"
          />
        </el-form-item>
        <el-form-item label="所属校区" prop="campusId">
          <campusSelect v-model="form.campusId" class="form-select" />
        </el-form-item>

        <el-form-item label="合同属性" prop="contractAttribute">
          <el-select
            v-model="form.contractAttribute"
            placeholder="请选择合同属性"
            clearable
            class="form-select"
          >
            <el-option label="新签" :value="0" />
            <el-option label="续费" :value="1" />
            <el-option label="赠送" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="合同类型" prop="contractType">
          <el-select
            v-model="form.contractType"
            placeholder="请选择确认状态"
            clearable
            class="form-select"
          >
            <el-option
              v-for="item in dictType"
              :label="item.dictLabel"
              :value="item.dictCode"
              :key="item.dictCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="开始日期" prop="beginTime">
          <el-date-picker
            clearable
            v-model="form.beginTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始日期"
            class="form-date-picker"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="周数" prop="week">
          <el-input-number
            v-model="form.week"
            controls-position="right"
            :min="1"
            class="form-input"
          />
        </el-form-item>
        <el-form-item label="是否推荐" prop="isReferrer">
          <el-select
            v-model="form.isReferrer"
            placeholder="请选择是否推荐"
            clearable
            class="form-select"
          >
            <el-option label="是" :value="1" />
            <el-option label="否" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="推荐人" v-if="form.isReferrer == 1">
          <el-input
            v-model="form.referrerId"
            placeholder="请输入推荐人"
            clearable
            class="form-input"
          />
        </el-form-item>
        <el-form-item label="签约者" prop="signerId">
          <userSelect v-model="form.signerId" class="form-select" />
        </el-form-item>
        <el-form-item label="签约日期" prop="signerTime">
          <el-date-picker
            clearable
            v-model="form.signerTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择签约日期"
            class="form-date-picker"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="续约可能">
          <el-select
            v-model="queryParams.isReferrer"
            placeholder="请选择续约可能"
            clearable
            class="form-select"
          >
            <el-option label="小" :value="0" />
            <el-option label="中" :value="1" />
            <el-option label="大" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input
            type="textarea"
            v-model="form.note"
            placeholder="请输入备注"
            :autosize="{ minRows: 4, maxRows: 4 }"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContract, getContract, addContract } from "@/api/system/contract";
import { getList } from "@/api/leadManagement/leadClientList";
import campusSelect from "@/components/Select/campusSelect.vue";
import userSelect from "@/components/Select/userSelect.vue";
export default {
  name: "Contract",
  components: {
    campusSelect,
    userSelect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,

      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同表格数据
      contractList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {
        week: 1,
      },

      // 年级选项
      gradeOpiton: [],
      //合同类型
      dictType: [],
      // 表单验证规则
      rules: {
        customerId: [
          { required: true, message: "请选择线索客户", trigger: "blur" },
        ],
        contractCode: [
          { required: true, message: "请输入合同编号", trigger: "blur" },
        ],
        campusId: [
          { required: true, message: "请选择所属校区", trigger: "blur" },
        ],
        contractAttribute: [
          { required: true, message: "请选择合同属性", trigger: "blur" },
        ],
        contractType: [
          { required: true, message: "请选择合同类型", trigger: "blur" },
        ],
        beginTime: [
          { required: true, message: "请选择开始日期", trigger: "blur" },
        ],
        week: [{ required: true, message: "请输入周数", trigger: "blur" }],
        isReferrer: [
          { required: true, message: "请选择是否推荐", trigger: "blur" },
        ],
        signerId: [
          { required: true, message: "请选择签约者", trigger: "blur" },
        ],
        signerTime: [
          { required: true, message: "请选择签约日期", trigger: "blur" },
        ],
      },
      //用户列表
      customerList: [],
    };
  },
  created() {
    this.gradeOpiton = [
      { value: 0, label: "小一" },
      { value: 1, label: "小二" },
      { value: 2, label: "小三" },
      { value: 3, label: "小四" },
      { value: 4, label: "小五" },
      { value: 5, label: "小六" },
      { value: 6, label: "初一" },
      { value: 7, label: "初二" },
      { value: 8, label: "初三" },
      { value: 9, label: "高一" },
      { value: 10, label: "高二" },
      { value: 11, label: "高三" },
      { value: 12, label: "高四" },
    ];

    this.getList();
    // 获取合同列表

    //获取线索用户
    this.getCustomerList();
    //获取字典
    this.getDictType();
  },
  methods: {
    /** 查询合同列表 */
    getList() {
      this.loading = true;
      listContract(this.queryParams).then((response) => {
        this.contractList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    getCustomerList() {
      getList().then((res) => {
        this.customerList = res.rows;
      });
    },

    getDictType() {
      this.getDicts("sys_contract_type").then((res) => {
        this.dictType = res.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        week: 1,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合同";
    },
    /** 修改按钮操作 */
    handleDetail({ id }) {
      this.$router.push({
        path: "/contract/detail",
        query: { id },
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateContract(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContract(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除合同编号为"' + ids + '"的数据项？')
        .then(function () {
          return delContract(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/contract/export",
        {
          ...this.queryParams,
        },
        `contract_${new Date().getTime()}.xlsx`
      );
    },

    formatGrade(grade) {
      return _.find(this.gradeOpiton, { value: grade })?.label || "未知年级";
    },
  },
};
</script>
<style lang="scss" scoped>
.form {
  &-input {
    width: 400px;
  }
  &-select {
    width: 400px;
  }

  &-date-picker {
    width: 400px;
  }
}
</style>
